﻿using System.Collections.Generic;
using System.Data;
using MedicService.Domain.Directory;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory {
    public class ComissionMemberFactory : EntityFactory<ComissionMember>{

        public ComissionMemberFactory()
            : base("ComissionMember", "member") {
        }

        protected override void FillEntity(ComissionMember entity, DataRow myDataRow) {
            entity.Id = (int)myDataRow["id"];
            entity.Member = myDataRow["member"] as string;
            entity.Comission = new ComissionFactory().Load(myDataRow["ComissionId"]);
        }

        protected override string GetInsertQuery(ComissionMember entity) {
            return "INSERT INTO [" + TableName + "]([member],[ComissionId])VALUES(" + Nullable(entity.Member) + ","+Nullable(entity.Comission)+");";
        }

        protected override string GetUpdateQuery(ComissionMember entity) {
            return "UPDATE [" + TableName + "] "
                + " SET "
                + " member=" + Nullable(entity.Member)
                + ", ComissionId=" + Nullable(entity.Comission)
                + " WHERE id = " + entity.Id + ";";
        }

        protected override ComissionMember CreateEntity() {
            return new ComissionMember();
        }

        public List<ComissionMember> GetList(Comission comission) {
            var entities = new List<ComissionMember>();
            var dataTable = DatabaseConnector.GetData(TableName, OrderBy, " ComissionId=" + comission.Id);
            foreach (DataRow myDataRow in dataTable.Rows) {
                var entity = CreateEntity();
                FillEntity(entity, myDataRow);
                entities.Add(entity);
            }
            return entities;
        }

    }
}